<template>
<h2>提示:如果录入了错误的学生班级信息请前往管理员页面进行修改</h2>
 <el-form
      :model="form"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item
        label="学生姓名"
        :label-width="formLabelWidth"
        prop="s_name"
      >
        <el-input v-model="form.s_name" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="性别" :label-width="formLabelWidth" prop="s_sex">
        <el-radio-group v-model="form.s_sex">
          <el-radio label="男">男</el-radio>
          <el-radio label="女">女</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item
        label="联系方式"
        :label-width="formLabelWidth"
        prop="s_lianxi"
      >
        <el-input
          v-model="form.s_lianxi"
          autocomplete="off"
          :maxlength="11"
        ></el-input>
      </el-form-item>
      <el-form-item label="年龄" :label-width="formLabelWidth" prop="s_age">
        <el-input v-model="form.s_age" autocomplete="off" :maxlength="2"></el-input>
      </el-form-item>
      <el-form-item
        label="班级名称"
        :label-width="formLabelWidth"
        prop="b_name"
      >
        <el-input v-model="form.b_name" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="学号" :label-width="formLabelWidth" prop="s_xuehao">
        <el-input
          v-model="form.s_xuehao"
          autocomplete="off"
          :maxlength="6"
        ></el-input>
      </el-form-item>
      <el-form-item label="民族" :label-width="formLabelWidth" prop="s_minzu">
        <el-input v-model="form.s_minzu" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item
        label="家庭住址"
        :label-width="formLabelWidth"
        prop="s_dizhi"
      >
        <el-input v-model="form.s_dizhi" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item
        label="身份证号"
        :label-width="formLabelWidth"
        prop="s_shenfenzheng"
      >
        <el-input
          v-model="form.s_shenfenzheng"
          autocomplete="off"
          maxlength="18"
        ></el-input>
      </el-form-item>
      <el-form-item  style="text-align: center;">
      <el-button type="primary"  @click="zengjia('ruleForm')"
        >录入</el-button
      >
    </el-form-item>
    </el-form>

</template>

<script>
const axios = require("axios");
export default {
  name:"teacher_luruxuesheng",
  data(){
      return{
          t_bianhao:"",
          form: {
        b_name: "",
        s_age: "",
        s_dizhi: "",
        s_lianxi: "",
        s_minzu: "",
        s_sex: "",
        s_name: "",
        s_shenfenzheng: "",
        s_xuehao: "",
      },
      rules: {
        s_name: [
          { required: true, message: "请输入学生姓名", trigger: "blur" },
          {
            min: 2,
            max: 30,
            message: "长度在 2 到 30 个字符",
            trigger: "blur",
          },
        ],
        s_lianxi: [
          { required: true, message: "请输入学生联系方式", trigger: "blur" },
          {
            min: 11,
            max: 11,
            message: "长度是 11 个字符",
            trigger: "blur",
          },
        ],
        s_age: [
          { required: true, message: "请输入学生年龄", trigger: "blur" },
          {
            min: 2,
            max: 2,
            message: "长度是 2 个字符",
            trigger: "blur",
          },
        ],
        s_xuehao: [
          { required: true, message: "请输入学生学号", trigger: "blur" },
          {
            min: 6,
            max: 6,
            message: "长度是 6 个字符",
            trigger: "blur",
          },
        ],
        b_name: [
          { required: true, message: "请输入班级名称", trigger: "blur" },
          {
            min: 4,
            max: 10,
            message: "长度在 4 到 10 个字符",
            trigger: "blur",
          },
        ],
        s_dizhi: [
          { required: true, message: "请输入学生家庭住址", trigger: "blur" },
          {
            min: 6,
            max: 20,
            message: "长度在 6 到 20 个字符",
            trigger: "blur",
          },
        ],
        s_minzu: [
          { required: true, message: "请输入学生民族", trigger: "blur" },
          {
            min: 2,
            max: 6,
            message: "长度在 2 到 6 个字符",
            trigger: "blur",
          },
        ],
        s_shenfenzheng: [
          { required: true, message: "请输入学生身份证号", trigger: "blur" },
          {
            min: 18,
            max: 18,
            message: "长度是 18 个字符",
            trigger: "blur",
          },
        ],
        s_sex: [
          { required: true, message: "请选择学生性别", trigger: "blur" },
          {
            trigger: "blur",
          },
        ],
      },
      }
  },
  methods:{
      zengjia(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let _this = this;
          _this.form.s_age = parseInt(_this.form.s_age);
          console.log(this.form);
          debugger;
          axios
            .get("https://localhost:44391/api/zhuce/chaxunxh", {
              params: {
                s_xuehao: this.form.s_xuehao,
              },
            })
            .then(function (response) {
              debugger;
              console.log(response);
              if (response.data != "") {
                _this.$notify({
                  title: "警告",
                  message: "学生学号重复！请检查学生学号！",
                  type: "warning",
                });
              }
              if (response.data === "") {
                console.log(_this.form);
                debugger;
                //   _this.form.t_age = parseInt(_this.form.t_age);
                _this.doZengjia();
              }
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    doZengjia() {
      let _this = this;
      console.log("123", this.form);
      debugger
      axios
        .get(
          "https://localhost:44391/api/guanliyuan/chaxunbanji",{
          params:{
            bname: this.form.b_name,
          },
          })
        .then(function (response) {
          console.log(response.data);
          if(response.data!=""){
        axios
        .post(
          "https://localhost:44391/api/teacher/xinzengxuesheng",
          _this.form
        )
        .then(function (response) {
          console.log(response);
          if (response.data >= 1) {
            _this.$notify({
              title: "成功",
              message: "录入成功",
              type: "success",
            });
            _this.$router.push({ path: "/teacher/teacher_xueshengxinxi" });
          } else {
            _this.$notify({
              title: "警告",
              message: "录入失败",
              type: "warning",
            });
          }
        });
          }else{
            _this.$notify({
              title: "警告",
              message: "录入失败!未查询到该班级!请检查班级是否输入正确!",
              type: "warning",
            });
          }
          });
    },
  },
  mounted(){
      this.t_bianhao = this.$store.state.t_bianhao;
      if(this.t_bianhao==''){
        this.$router.push({ path: "/" });
      }
  }
}
</script>

<style scoped>
 h2{
   font-size: 20px;
   color: red;
   text-align: center;
   margin-top: 0px;
   margin-bottom: 0px;
 }
</style>